package com.mobilesales.android.adapters;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.mobilesales.android.R;
import com.mobilesales.android.model.clients.Client;

public class ClientArrayAdapter extends ArrayAdapter<Client> {
	private List<Client> clients = null;

	public ClientArrayAdapter(Context context, int textViewResourceId,
			List<Client> objects) {
		super(context, textViewResourceId, objects);
		this.clients = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
					"layout_inflater");
			v = vi.inflate(R.layout.client_list_item, null);
		}

		Client client = null;
		if ((position >= 0) && (position < this.clients.size())) {
			client = clients.get(position);
		}

		if (client != null) {
			TextView nameField = (TextView) v.findViewById(R.id.clientName);
			nameField.setText(client.Name);
			TextView addressField = (TextView) v
					.findViewById(R.id.clientAddress);
			String addr = String.format("%s, %s %s", new Object[] {
					client.Street, client.Zipcode, client.City });
			addressField.setText(addr);
			TextView nipField = (TextView) v.findViewById(R.id.clientNip);
			nipField.setText(client.Nip);
		}
		return v;
	}
}